#include "Gomoku.h"


//将棋盘进行初始化
char** Board_Init() {
    char **board = (char **)malloc(Borard_Size * sizeof(char *));
    for (int i = 0; i <= Borard_Size; i++) {
        board[i] = (char *)malloc(Borard_Size * sizeof(char));
    }

    for (int i = 0; i < Borard_Size; i++) {
        for (int j = 0; j < Borard_Size; j++) {
            board[i][j] = '+';
        }
    }
    return board;
}

void printf_Board(char **board){
    printf("\n");
    printf("      1    2    3    4    5    6    7    8    9   10   11   12   13   14   15");
    printf("\n");
    for (int i = 0; i < Borard_Size; i++)
    {   
        if (i<9)
        {
            printf("    %d ",i+1);
        }else{
            printf("   %d ",i+1);
        }
        
        
        for (int j = 0; j < Borard_Size; j++)
        {   
	    if(board[i][j]=='T')printf("\033[1;44;34m%c\033[0m",board[i][j]);
	    else if(board[i][j]=='X')printf("\033[1;47;37m%c\033[0m",board[i][j]);
	    else printf("%c",board[i][j]);
            
            if (j!=14) printf("————");
        }
        printf("\n");
        if (i!=14) printf("      |    |    |    |    |    |    |    |    |    |    |    |    |    |    |");
        printf("\n");
    }
    
}

int Draw_Board(char ** board,int row,int col,int player){
    char laout_char;
    if (player==1) laout_char='X';
    if (player==2) laout_char='T';
    row--;
    col--;

    //判断是否越界
    if (row>Borard_Size||col>Borard_Size) {
        printf("输入的坐标越界了\n");
        return 0;
    }

    //判断是否已经有棋子
    if (board[row][col]!='+'){
        printf("该位置已经有棋子了\n");
        return 0;
    }

    //放置棋子
    board[row][col]=laout_char;
    return 1;
}

int Judge_wins(char **board,int row,int col,int player){
    int temp=1;
    char laout_char=player==1?'X':'T';
    int row_Judge=row-1;
    int col_Judge=col-1;

    //判断横向
    while (col_Judge!=0&&board[row_Judge][--col_Judge]==laout_char)temp++;
    row_Judge=row-1;
    col_Judge=col-1;
    while (col_Judge!=Borard_Size&&board[row_Judge][++col_Judge]==laout_char)temp++;
    if (temp>=5) return 1;

    row_Judge=row-1;
    col_Judge=col-1;
    temp=1;
    
    //判断纵向
    while (row_Judge!=0&&board[--row_Judge][col_Judge]==laout_char)temp++;
    row_Judge=row-1;
    col_Judge=col-1;
    while (row_Judge!=Borard_Size&&board[++row_Judge][col_Judge]==laout_char)temp++;
    if (temp>=5) return 1;

    row_Judge=row-1;
    col_Judge=col-1;
    temp=1;

    //判断左上到右下
    while (row_Judge!=0&&row_Judge!=Borard_Size&&col_Judge!=0&&col_Judge!=Borard_Size&&board[--row_Judge][--col_Judge]==laout_char)temp++;
    row_Judge=row-1;
    col_Judge=col-1;
    while (row_Judge!=0&&row_Judge!=Borard_Size&&col_Judge!=0&&col_Judge!=Borard_Size&&board[++row_Judge][++col_Judge]==laout_char)temp++;
    if (temp>=5) return 1;

    row_Judge=row-1;
    col_Judge=col-1;
    temp=1;

    //判断右上到左下
    while(row_Judge!=0&&row_Judge!=Borard_Size&&col_Judge!=0&&col_Judge!=Borard_Size&&board[--row_Judge][++col_Judge]==laout_char)temp++;
    row_Judge=row-1;
    col_Judge=col-1;
    while (row_Judge!=0&&row_Judge!=Borard_Size&&col_Judge!=0&&col_Judge!=Borard_Size&&board[++row_Judge][--col_Judge]==laout_char)temp++;
    if (temp>=5) return 1;

    return 0;
}
